#!/bin/sh

test_description="test proc-receive"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
  REPO_ID=$(find_repo | jq .id)
  if [ -n $REPO_ID ]; then
    delete_repo > /dev/null 2>&1
    test_expect_code 0
  fi

  REPO_ID=$(create_repo) &&
  add_member $WRITE_LEVEL $another_user_external &&
  init_repo
  test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
  echo "This is the first commit" > "$REPO_DIR"/README &&
  git -C "$REPO_DIR" add README &&
  git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master > test-2 2>&1
'
#new模式创建mr成功
test_expect_success "[test-3][new] creating new mr success" '
  add_member $READ_LEVEL $another_user_external &&
  echo "[test-5][new] creating new mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "creating new mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=new > test-3 2>&1 &&
  grep "has been created" test-3
'

#继续创建新mr成功
test_expect_success "[test-4][new] continue create new cr success" '
  echo "[test-4][new] continue create new cr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "continue create new cr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=new > test-4 2>&1
  grep "has been created" test-4
'

#以下针对保护分支的的测试
#读权限推保护分支创建mr
test_expect_success "[test-5][new][protected] read permission push to protected branch create mr success" '
  add_member $READ_LEVEL $another_user_external &&
  protect_branch master &&
  echo "[test-5][new][protected] read permission push to protected branch create mr success" >> "$REPO_DIR"/test.file &&
  git -C "$REPO_DIR" add test.file &&
  git -C "$REPO_DIR" commit -m "read permission push to protected branch create mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
  git -C "$REPO_DIR" push origin master -o review=new > test-5 2>&1 &&
  grep "has been created" test-5
'

test_expect_success "[test-6] clean up" '
  delete_repo "$REPO_ID" > test-6 2>&1
'

test_done